﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using TravelingWebApp.Models;
using TravelingWebApp.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Hosting;
using TravelingWebApp.Data;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Identity;
using System.Security.Claims;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace TravelingWebApp.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class FunctionsController : Controller
    {
        private TravelingContext _context;
        private UserManager<User> _userManager;
        private readonly IHostingEnvironment _hostingEnvironment;

        public FunctionsController(TravelingContext context, IHostingEnvironment environment, UserManager<User> userManager)
        {
            _context = context;
            _hostingEnvironment = environment;
            _userManager = userManager;
        }
        
        //预定，需要登录USER账户
        [HttpPost]
        [Route("Book")]
        [Authorize(Roles = "USER")]
        public async Task<ActionResult<string>> Booking(string id, int count, bool hasGuide, DateTime date)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(Json("ModelState InValid").StatusCode = -1);
            }
            if (_context.DbUsers.Include(b => b.Id).Count<User>() == 0)
            {
                return BadRequest(Json("User Not Found").StatusCode = 0);
            }
            var bill = new Bill
            {
                Date = date,
                Count = count,
                IsValid = true,
                User = _context.DbUsers.Include(b => b.Id).SingleOrDefault(c => c.Id == id)
            };
            _context.Add(bill);
            await _context.SaveChangesAsync();

            return Ok(Json("Ok"));
        }

        //评论，需要登录USER账户
        [HttpPost]
        [Route("AddComment")]
        [Authorize(Roles = "USER")]
        public async Task<ActionResult<string>> AddComment([FromBody]CommentView commentView)
        {
            User user = await _userManager.GetUserAsync(HttpContext.User);
            if(user == null)
            {
                return BadRequest(Json("Please login"));
            }

            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("", "Faild to AddAnnouncement");
                return BadRequest("ModelState InValid");
            }

            var comment = new Comment
            {
                User = user,
                Text = commentView.Text,
                Title = null,
                ScenaryName = commentView.ScenaryName,
                Stars = commentView.Stars,
                Date = DateTime.Now,
                IsValid = true,
            };
        
            _context.DbComments.Add(comment);
            await _context.SaveChangesAsync();

            return Ok(Json(new { msg = "Add success " }));
        }



    }
}
